package demo4.mapper;

import demo4.pojo.UserInfo;
import demo4.util.DBUtil;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Component

public class UserMapper implements IUserMapper {
    @Autowired
    private IDBUtil dbutil;

    @Override
    public int insert(UserInfo userInfo) {
        System.out.println("UserMapper:插入User表");
        try (Connection connection = dbutil.getConnection()) {
            String sql = "INSERT INTO userinfo(username,password,nickname,age) VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, userInfo.getUsername());
            ps.setString(2, userInfo.getPassword());
            ps.setString(3, userInfo.getNickname());
            ps.setInt(4, userInfo.getAge());

            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    public boolean query(UserInfo userInfo) {
        try (Connection connection = dbutil.getConnection()) {
            String sql = "SELECT username,password,nickname,age FROM userinfo WHERE username = ? AND password = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, userInfo.getUsername());
            ps.setString(2, userInfo.getPassword());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                userInfo.setNickname(rs.getString("nickname"));
                userInfo.setAge(rs.getInt("age"));
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    //    public UserInfo message(UserInfo userInfo) {
//        try (Connection connection = dbutil.getConnection();) {
//            String sql = "SELECT username,password,nickname,age FROM userinfo ";
//            PreparedStatement ps = connection.prepareStatement(sql);
//            ResultSet rs = ps.executeQuery();
//            while (rs.next()) {
//                String username = rs.getString("username");
//                String password = rs.getString("password");
//                String nickname = rs.getString("nickname");
//                int age = rs.getInt("age");
//                System.out.println(username+"-"+password+"-"+nickname+"-"+age);
//            }
//            return userInfo;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
    public List<UserInfo> selectAll() {
        List<UserInfo> userList = new ArrayList<>();
        try (Connection connection = dbutil.getConnection();) {
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM userinfo";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                UserInfo userInfo = new UserInfo();
                userInfo.setId(rs.getInt("id"));
                userInfo.setUsername(rs.getString("username"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setNickname(rs.getString("nickname"));
                userInfo.setAge(rs.getInt("age"));
                userList.add(userInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }
}
